<template>
	<view v-if="goodsDetail.goods_name" class="goods-detail-container">
		<!-- 轮播图部分 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in goodsDetail.pics" @click="preview(i)">
        <image class="swiper-item" :src="item.pics_big"></image>
      </swiper-item>
    </swiper>
	<!-- 商品信息部分 -->
  <view class="goods-info-box">
    <view class="goods-price">
      ￥{{ goodsDetail.goods_price }}
    </view>
    <view class="goods-body">
      <view class="goods-name">{{goodsDetail.goods_name}}</view>
      <view class="fav">
        <uni-icons type="star" size="18" color="gray"></uni-icons>
        <text>收藏</text>
      </view>
    </view>
    <view class="yf">
      快递：免运费
    </view>
  </view>
  <!-- 商品详情模块 -->
  <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
  <!-- 商品导航部分 -->
  <view class="goods-nav">
    <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
  </view>
  </view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
    computed:{
      ...mapState('cart', ['cart']),
      ...mapGetters('cart', ['total'])
    },
    watch: {
      // total (newval) {
      //   this.options.find(x => x.text === "购物车").info = newval
      // }
      
      // 解决页面加载时的不能立即执行问题
      total: {
        handler(newval){
           this.options.find(x => x.text === "购物车").info = newval
        },
        immediate: true
      }
    },
		data() {
			return {
				// 商品详情
        goodsDetail: {},
        // 商品ID
        goods_id: null,
        options: [{
                    icon: 'shop',
                    text: '店铺',
                    infoBackgroundColor:'#007aff',
                    infoColor:"red"
                }, {
                    icon: 'cart',
                    text: '购物车',
                    info: 0
                }],
                buttonGroup: [{
                  text: '加入购物车',
                  backgroundColor: '#ff0000',
                  color: '#fff'
                },
                {
                  text: '立即购买',
                  backgroundColor: '#ffa200',
                  color: '#fff'
                }
                ]
			}
		},
    onLoad (options) {
      // console.log(options)
      this.goods_id = options.goods_id
      // 同步购车商品数量
      // this.options.find(x => x.text === "购物车").info = this.total
      this.getGoodsDetail()
    },
		methods: {
			// 获取商品详情
      async getGoodsDetail () {
        const { data: res } = await uni.$http.get('/goods/detail?', {goods_id: this.goods_id})
        // console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg("获取商品详情失败")
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, "<img style='display: block; '").replace(/webp/g, 'jpg')
        this.goodsDetail = res.message
      },
      // 监听轮播图的点击事件
      preview (i) {
        uni.previewImage({
          current: i,
          urls: this.goodsDetail.pics.map( x => x.pics_big)
        })
      },
      // 监听底部左侧按钮点击事件
      onClick (e) {
        console.log(e.content.text)
        if(e.content.text === "购物车") {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      
      ...mapMutations('cart', ['addToCart']),
      buttonClick (e) {
        // console.log(e)
        if(e.content.text === "加入购物车") {
           // 2. 组织一个商品的信息对象
                const goods = {
                   goods_id: this.goodsDetail.goods_id,       // 商品的Id
                   goods_name: this.goodsDetail.goods_name,   // 商品的名称
                   goods_price: this.goodsDetail.goods_price, // 商品的价格
                   goods_count: 1,                           // 商品的数量
                   goods_small_logo: this.goodsDetail.goods_small_logo, // 商品的图片
                   goods_state: true                         // 商品的勾选状态
                }
          this.addToCart(goods)
          // this.options[1].info = this.cart.length
          console.log(this.cart)
      }
    
    }
	},
  }
</script>

<style lang="scss" scoped>
swiper {
  height: 750rpx;
  image {
    width: 100%;
    height: 100%;
  }
}
.goods-info-box {
  padding: 10px;
  padding-right: 0;
  .goods-price {
    margin: 10px 0;
    color: #c00000;
    font-size: 18px;
  }
  .goods-body {
    display: flex;
    justify-content: space-between;
    .goods-name {
      font-size: 13px;
      margin-right: 10px;
    }
    .fav {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 120px;
      border-left: 1px solid #efefef;
      font-size: 12px;
      color: gray;
    }
  }
  .yf {
    font-size: 12px;
    color: gray;
    margin: 10px 0;
  }
}
.goods-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
.goods-detail-container {
  padding-bottom: 50px;
}
</style>
